package edu.xidian.forum.common;

import java.io.Serializable;

public class Result<T> implements Serializable {
    private final int code;
    private final String message;
    private final T data;

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(Error error) {
        this(error.getCode(), error.getMessage(), null);
    }

    public Result(Error error, T data) {
        this(error.getCode(), error.getMessage(), data);
    }

    public Result(Error error, String message, T data) {
        this(error.getCode(), message, data);
    }

    public static <T> Result<T> success() {
        return new Result<>(Errors.OK);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(Errors.OK, data);
    }

    public static <T> Result<T> success(String message, T data) {
        return new Result<>(Errors.OK, message, data);
    }

    public static <T> Result<T> failed() {
        return new Result<>(Errors.FAILED);
    }

    public static <T> Result<T> failed(String message) {
        return new Result<>(Errors.FAILED, message, null);
    }

    public static <T> Result<T> badRequest() {
        return new Result<>(Errors.BAD_REQUEST);
    }

    public static <T> Result<T> badRequest(String message) {
        return new Result<>(Errors.BAD_REQUEST, message, null);
    }

    public static <T> Result<T> unauthorized() {
        return new Result<>(Errors.UNAUTHORIZED);
    }

    public static <T> Result<T> unauthorized(T data) {
        return new Result<>(Errors.UNAUTHORIZED, data);
    }

    public static <T> Result<T> forbidden() {
        return new Result<>(Errors.FORBIDDEN);
    }

    public static <T> Result<T> forbidden(T data) {
        return new Result<>(Errors.FORBIDDEN, data);
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
